/*
 * @Descripttion: unTitle
 * @Author: yizheng.yuan
 * @Date: 2021-11-16 10:20:44
 * @LastEditors: yizheng.yuan
 * @LastEditTime: 2021-11-16 11:57:50
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'

import User from '../components/User.vue'
import Right from '../components/Right.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    children:[
      {
        path: '/user',
        name: 'User',
        component: User,
      },
      {
        path: '/right',
        name: 'Right',
        component: Right,
      }
    ]
  },
  {
    path: '/login',
    name: 'Login',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "Login" */ '../views/Login.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

// 这里写一个全局路由守卫：作用：如果尚未登录，就只能去到登录页面
router.beforeEach((to,from,next)=>{
  console.log('to,from,next',to,from,next)
  if(to.path == '/login'){
    next()
  }else{
    var username = window.localStorage.getItem('username');
    console.error('获取到的username', username)
    if(username){
      next()
    }else{
      // 如果没有username，就表示没有登录
      next('/login')
    }
  }
})

export default router
